package version.test.service.impl.es;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import version.test.dao.WeaponMapper;
import version.test.dto.PageInfo;
import version.test.dto.entity.WeaponEntity;
import version.test.service.ESWeaponService;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @description:
 * @author: hzw
 * @time: 2023/11/10 17:47
 */
@Service
public class WeaponESService {
    @Autowired
    private ESWeaponService esWeaponService;


    public WeaponEntity get(Long id) {
        Optional<WeaponEntity> optional = esWeaponService.findById(id);
        return optional.orElse(null);
    }

    public void save(WeaponEntity weaponEntity) {
        esWeaponService.save(weaponEntity);
    }

    public List<WeaponEntity> findAll(){
        List<WeaponEntity> result = new ArrayList<>();
        Iterable<WeaponEntity> all = esWeaponService.findAll();
        all.forEach(result::add);
        return result;
    }

    public Page<WeaponEntity> page(WeaponEntity entity){
        //设置排序(排序方式，正序还是倒序，排序的 id)
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        int currentPage = 0;
        //每页显示的条数
        int pageSize = 10;
        //设置分页
        PageRequest pageable = PageRequest.of(currentPage, pageSize, sort);
        Page<WeaponEntity> page = esWeaponService.findAll(pageable);
        return page;
    }

    public List<WeaponEntity> findByName(String name) {

        return esWeaponService.findByName(name);
    }

    public Page<WeaponEntity> pageByName(String name) {
        //设置排序(排序方式，正序还是倒序，排序的 id)
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        int currentPage = 0;
        //每页显示的条数
        int pageSize = 10;
        //设置分页
        PageRequest pageable = PageRequest.of(currentPage, pageSize, sort);
        Page<WeaponEntity> page = esWeaponService.findByName(name, pageable);
        return page;
    }

    public List<WeaponEntity> findByCodeOrName(String param) {

        return esWeaponService.findByCodeLikeOrNameLike(param, param);
    }
}
